import { getToken, setToken, removeToken } from '@/utils/TokenOpers'
import { login,getUserInfo } from "@/axios/api"
const user = {
  state: {
    token: getToken(),
    avatar:null,
    title:'AutoSC',//头title
    dataList:[],//文件数据
    // exlList:[],//外部数据源
    // notification:false,
    // resultUrlDict:{}
  },
  getters:{
    avatar:state => state.avatar,
    title: state => state.title,
    token: state => state.token,
    dataList: state => state.dataList,
    // exlList: state => state.exlList,
    // resultURL: state => key => state.resultUrlDict[key]
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_TITLE: (state, title) => {
      state.title = title
    },
    setAvatar:(state,ava) =>{
      state.avatar = ava
    },
    setDataList:(state, dataList) => {
      state.dataList = dataList
    },
    /*    setExlList: (state, exlList) => {
      state.exlList = exlList
    }, */
    addDataList:(state, data) => {
      state.dataList.unshift(data)
    },
    /*    addExlList: (state, exlList) => {
      state.exlList = exlList
    }, */
    delDataList:(state, index) =>{
      state.dataList.splice(index, 1)
    },
    /*    delExlList: (state, index) => {
      state.exlList.splice(index, 1)
    }, */
    changeDataList:(state,data)=>{
      state.dataList[data.index] = data.data
    },
    // changeResultDict:(state,dict)=>{//add && uodate
    //   state.resultUrlDict[dict.key] = dict.value
    // },
    changeDataListInfo: (state, data) => {
      state.dataList[data.index].info = data.info
    }
    
  },
  actions: {
    // changeResultDict({commit},dict){
    //   commit('changeResultDict', dict)
    // },
    delDataList({commit},index){
      commit('delDataList', index)
    },
    changeDataListInfo({ commit }, info){
      commit('changeDataListInfo', info)
    },
    //*{index,data}
    changeDataList({ commit }, data){
      commit('changeDataList', data)
    },
    //*{index,info}
    addDataList({ commit },data){
      commit('addDataList', data)
    },
    // 登录
    Login({ commit }, userInfo) {
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        login({ username:username.trim(),password,})
        .then(response => {
          const { token } = response.data
          console.log(`token:${token}`)
          commit('SET_TOKEN', token)
          setToken(token)
          resolve('登录成功')
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo({token:state.token}).then(response => {
          const { data } = response
          console.log(data)
          if (!data) {
            removeToken()
            reject('请重新登录')
          }
          if(data.avatar)
            commit('setAvatar',data.avatar)
          commit('setDataList', data.dataList.reverse())
          // .map(e=>{
          //   let {analysis,target,feature} = e.info
          //   e.info = {
          //     analysis,
          //     target:Boolean(target),
          //   }
          //   return e
          // }))
          // const {username, profile_path} = data
          // commit('setInfo', data)
          // commit('setRealName', username)
          // commit('setAvatar', profile_path)
          // localStorage.setItem('avatar', profile_path)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}
export default user